 package com.edu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.demo.entity.*;
import com.edu.demo.exception.MyNullPointerException;
import com.edu.demo.mapper.ContractMapper;
import com.edu.demo.mapper.ProResumeMapper;
import com.edu.demo.mapper.ProjectMapper;
import com.edu.demo.mapper.StudentMapper;
import com.edu.demo.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.demo.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 合同表 服务实现类
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private ProResumeMapper resumeMapper;

    @Autowired
    ProjectMapper projectMapper;


    @Autowired
    private StudentMapper studentMapper;

    /**
     * 添加合同：教师录用学生
     *
     * @param contract
     * @return
     */
    @Override
    public boolean addContract(Contract contract) throws BadCredentialsException{
        Teacher teacher = SecurityUtils.getTeacher();

        Project project = projectMapper.selectById(contract.getProjectId());
        if(Objects.isNull(project) || project.gettId()!=teacher.getId())
            throw new BadCredentialsException("只能签订自己的项目");

        contract.setProName(project.getTitle());
        ProResume proResume = new ProResume();
        proResume.setStuId(contract.getStuId());

        proResume.setProjectId(contract.getProjectId());
        proResume.setStatus(3);
        QueryWrapper<ProResume> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("project_id",contract.getProjectId());
        updateWrapper.eq("stu_id",contract.getStuId());
        resumeMapper.update(proResume, updateWrapper);

        contract.setSignTime(new Date());

        int i = contractMapper.insert(contract);
        return i>0;
    }


    @Override
    public Map<String, Object> list(Integer projectId) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId);
        List<Contract> contracts = contractMapper.selectList(queryWrapper);
        List<Integer> ids = contracts.stream().map(contract -> contract.getStuId()).collect(Collectors.toList());
        QueryWrapper<Student> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id",ids);
        List<Student> students = studentMapper.selectList(queryWrapper1);
        for (int i = 0; i < contracts.size(); i++) {
            for (int i1 = 0; i1 < students.size(); i1++) {
                if(contracts.get(i).getStuId() == students.get(i1).getId()){
                    contracts.get(i).setStudent(students.get(i1));
                    students.get(i1).setPassword(null);
                }
            }
        }
        Project project = projectMapper.selectById(projectId);
        Map<String,Object> res = new HashMap<>();
        res.put("project",project);
        res.put("contracts",contracts);
        return res;
    }


    public Contract contractOfStudent(Integer projectId){
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId);
        queryWrapper.eq("stu_id",SecurityUtils.getStudent().getId());
        Contract contract = contractMapper.selectOne(queryWrapper);
        return contract;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Contract entity) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",entity.getProjectId());
        queryWrapper.eq("stu_id",entity.getStuId());

            int update = contractMapper.update(entity, queryWrapper);
            if(update>1) throw new MyNullPointerException("更新了多个合同");
            return update ==1;

    }
}
